package com.itic.system.user.balanceLog.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.type.FlowMethod;
import com.itic.appbase.framework.type.FlowSource;
import com.itic.appbase.framework.type.FlowType;
import com.itic.appbase.framework.type.OperateType;
import org.etsi.uri.x01903.v13.CRLIdentifierType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.system.user.balanceLog.persistence.dao.UserBalanceLogDao;
import com.itic.system.user.balanceLog.persistence.model.UserBalanceLog;
import com.itic.system.user.balanceLog.persistence.model.UserBalanceLogExample;
import com.itic.system.user.balanceLog.persistence.model.UserBalanceLogExample.Criteria;


import java.util.List;


@Service
@Transactional
public class UserBalanceLogService
     extends CrudService<UserBalanceLogDao,UserBalanceLog,UserBalanceLogExample> {
    @Autowired
    private UserBalanceLogDao userBalanceLogDao;

    /**
     *  初始化用户余额日志
     * @param userId
     * @param money
     * @param flowType
     * @param flowMethod
     * @param operateType
     * @return
     */
    public UserBalanceLog generalUserBalanceLog(String userId, double money, FlowType flowType , FlowMethod flowMethod , OperateType operateType){
        UserBalanceLog userBalanceLog = new UserBalanceLog();
        userBalanceLog.setUserId(userId);
        userBalanceLog.setMoney(money);
        userBalanceLog.setType(flowType.getType());
        userBalanceLog.setMethod(flowMethod.getMethod());
        userBalanceLog.setOperateType(operateType.toString());
        return userBalanceLog;
    }

    /**
     * 添加支付日志
     * @param userId
     * @param money
     * @param flowType
     * @param flowMethod
     * @param operateType
     * @param orderNo
     * @param flowSource
     * @param sourceUserId
     * @return
     */
    public String addIncome(String userId, double money, FlowType flowType , FlowMethod flowMethod , OperateType operateType,String orderNo,FlowSource flowSource ,String sourceUserId){
        UserBalanceLog userBalanceLog = this.generalUserBalanceLog(userId,money,flowType,flowMethod,operateType);
        userBalanceLog.setOrderNo(orderNo);
        userBalanceLog.setSource(flowSource.getSource());
        userBalanceLog.setSourceUserId(sourceUserId);
        return this.add(userBalanceLog);
    }

    /**
     * 添加支付余额日志
     * @param userId
     * @param money
     * @param flowType
     * @param flowMethod
     * @param operateType
     * @return
     */
    public UserBalanceLog addExPend(String userId, double money, FlowType flowType , FlowMethod flowMethod , OperateType operateType){
        UserBalanceLog userBalanceLog = this.generalUserBalanceLog(userId,money,flowType,flowMethod,operateType);
        String result = this.add(userBalanceLog);
        if(SUCCESS.equals(result))
            return userBalanceLog;
        else
            return null;
    }

    /**
     * 查询用户收入支出记录(分页)
     * @param example
     * @param userId
     * @param flowType
     * @param flowMethod
     * @param flowSource
     * @return
     */
    public List<UserBalanceLog> findUserBalanceByUserId(UserBalanceLogExample example,String userId,FlowType flowType,FlowMethod flowMethod ,FlowSource flowSource){
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andUserIdEqualTo(userId);
        criteria.andTypeEqualTo(flowType.getType());
        criteria.andMethodEqualTo(flowMethod.getMethod());
        criteria.andSourceEqualTo(flowSource.getSource());
        return this.list(example);
    }

    /**
     * 获取用户订单交易获取的总收益
     * @param userId
     * @return
     */
    public double getUserTotalIncomeMoney(String userId){
       Double money =  this.userBalanceLogDao.getUserTotalIncomeMoney(userId);
       return money ==null? 0:money.doubleValue();
    }

}